package htmodels

import (
	"github.com/astaxie/beego/orm"
	"choujiang/models"
	"time"
)

func HTUserDevicePointAdd(userdevicepoint *models.UserDevicePoint) error {
	o := orm.NewOrm()
	_, err := o.Insert(userdevicepoint)
	return err
}

func HTUserDevicePointGetDetail(userdevicepoint *models.UserDevicePoint) error {
	o := orm.NewOrm()
	return o.Read(userdevicepoint)
}

func HTUserDevicePointGetCount(search string, searchFiled string) (int64, error) {
	o := orm.NewOrm()
	m := models.UserDevicePoint{}
	count, err := o.QueryTable(m.TableName()).Filter(searchFiled+"__icontains", search).Count()
	return count, err
}

func HTUserDevicePointGetList(userdevicepoints *[]models.UserDevicePoint, fields []string, offset int64, limit int, search string, searchFiled string, orderby string) error {
	o := orm.NewOrm()
	m := models.UserDevicePoint{}
	_, err := o.QueryTable(m.TableName()).Filter(searchFiled+"__icontains", search).OrderBy(orderby).Offset(offset).Limit(limit).All(userdevicepoints, fields...)
	return err
}

func HTUserDevicePointDelete(Ids []int64) (int64, error) {
	o := orm.NewOrm()
	m := models.UserDevicePoint{}
	count, err := o.QueryTable(m.TableName()).Filter("Id"+"__in", Ids).Delete()
	return count, err
}
func HTUserDevicePointUpdate(userdevicepoint *models.UserDevicePoint) error {
	o := orm.NewOrm()
	_, err := o.Update(userdevicepoint)
	return err
}


func HTGetEfficientPoint(uid int,lasttime time.Time) (int, error) {
	var list orm.ParamsList
	o := orm.NewOrm()
	m := models.UserDevicePoint{UserId: uid}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_UserId(), uid).Filter(m.FN_GetTime()+"__gt", lasttime).ValuesFlat(&list, m.FN_Point())
	if err != nil {
		return 0, err
	}
	ret := 0
	for _, v := range list {
		ret += int(v.(int64))
	}
	return ret, nil
}
